﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using ServiceBase;

namespace ServicePrice
{
    public class www_yohobuy_com : IServicePrice
    {
        private Dictionary<string, char> _matrix = new Dictionary<string, char>();
        public www_yohobuy_com()
        {
            _matrix.Add("110011010010011110111111001100111111001100001100", '￥');
            _matrix.Add("00110111101100110011001100110011", '1');
            _matrix.Add("0111101000011100100100001000011100101110", '5');
            _matrix.Add("0011001001110001111011001110010100100110", '6');
            _matrix.Add("0000000000000011", '.');
            _matrix.Add("0111001001110011100111001110010100101110", '0');
            _matrix.Add("000110000110001110010110110110111111000110000110", '4');
            _matrix.Add("0111011011110110111011011110111101101110", '8');
            _matrix.Add("0111011001110011100101111000011100101110", '9');
            _matrix.Add("001100010010000010000010000100001000011000111111", '2');
            _matrix.Add("111111000010000100001100001000001000011000011000", '7');
            _matrix.Add("0111010010000100011000011000111001101110", '3');
        }
        /// <summary>
        /// 识别一张图片价格，并返回图片上的值。
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string DoRecognition(string url)
        {
            //下载图片
            Bitmap map = GetBitmap.From_URL(url);
            //处理成可识别的图片
            Bitmap[] list = Dispose(map);
            //识别
            string result = Match.Match_Dictionary.ByDictionary(list, _matrix);
            return result;
        }

        private Bitmap[] Dispose(Bitmap map)
        {
            //二值化
            SimpleColor.TwoColor1(map);
            //按空列切割
            Bitmap[] list = Split.ByEmptyColumn(map);
            //切割粘连片段
            list = Split.ByLimitWidth(list, 8, 6);
            //切割小数点
            list = SplitPoint(list);
            return list;
        }
        public Bitmap[] ViewMatrix(string dir)
        {
            Bitmap[] sourceList = GetBitmap.From_Folder(dir);
            List<Bitmap> all = new List<Bitmap>();
            foreach (Bitmap map in sourceList)
            {
                Bitmap[] list = Dispose(map);
                all.AddRange(list);
            }
            Bitmap[] matrix = ServiceMatrix.Pick_Matrix.ByBitmapList(all.ToArray());
            Helper_View.View.View_Bitmap(matrix);
            return matrix;
        }

        public void CreateMatrix(string dir)
        {
            Bitmap[] matrix = ViewMatrix(dir);
            ServiceMatrix.Builder_Matrix.Create(matrix, Guid.NewGuid().ToString() + ".txt", ServiceMatrix.Builder_Matrix.MatrixType.零一字符串);
        }

        /// <summary>
        /// 由于小数点有粘连，通过此方法切割之
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        private Bitmap[] SplitPoint(Bitmap[] list)
        {
            List<Bitmap> temp = new List<Bitmap>();
            foreach (Bitmap map in list)
            {
                if (map.Width > 6)
                {
                    foreach (Bitmap item in Split.ByXIndex(map,2))
                    {
                        temp.Add(item);
                    }
                }
                else
                {
                    temp.Add(map);
                }
            }
            return temp.ToArray();
        }
    }
}
